﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;
using System.Data.SqlClient;

namespace QLDT
{
    public partial class frm_DSGiangVien : DevExpress.XtraEditors.XtraForm
    {
        public bool TTThemSua = true;
        private string MaGV_cu;
        public frm_DSGiangVien()
        {
            InitializeComponent();
        }

        private void frm_DSGiangVien_Load(object sender, EventArgs e)
        {
            load();
        }

        private void load()
        {
            layoutMaCN_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutMaCN_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutMaKhoa_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            grid_GiangVien.DataSource = BUS_GiangVien.SelectGiangVien();
            txt_GVMaGV.Enabled = false;
            txt_GVTenGV.Enabled = false;
            txt_GVDienThoai.Enabled = false;
            txt_GVEmail.Enabled = false;
            txt_GVMaCN.Enabled = false;
            txt_DSGVCNBM.Enabled = false;
            txt_DSGVLCNC.Enabled = false;
            txt_DSGVMaKhoa.Enabled = false;
            radioGroup_gioitinh_GV.Enabled = false;
            simpleBt_Them.Enabled = true;
            simpleBt_Xoa.Enabled = true;
            simpleBt_Sua.Enabled = true;


            simpleBt_Capnhat.Enabled = simpleBt_Huy.Enabled = false;
            if (gv_GV.RowCount < 1)
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = false;
            }
            else
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = true;
            }

            Bindings();
            gridLookMaCN.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaCN.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaCN.Properties.ValueMember = "MaCN";
            gridLookMaCN.Properties.DisplayMember = "MaCN";
            gridLookMaCN.Properties.View.BestFitColumns();
            gridLookMaCN.Properties.PopupFormWidth = 400;
            gridLookMaCN.Properties.NullText = "Chọn Chuyên Ngành";
            gridLookMaCN.Properties.DataSource = BUS_ChuyenNganh.SelectChuyenNganh();


            gridLookMaKhoa.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaKhoa.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaKhoa.Properties.ValueMember = "MaKhoa";
            gridLookMaKhoa.Properties.DisplayMember = "MaKhoa";
            gridLookMaKhoa.Properties.View.BestFitColumns();
            gridLookMaKhoa.Properties.PopupFormWidth = 400;
            gridLookMaKhoa.Properties.NullText = "Chọn Mã Khoa";
            gridLookMaKhoa.Properties.DataSource = BUS_Khoa.SelectKhoa();

            if (gv_GV.GetRowCellValue(gv_GV.FocusedRowHandle, "GioiTinh") == null)
            {
                return;
            }
            if (gv_GV.GetRowCellValue(gv_GV.FocusedRowHandle, "GioiTinh").ToString() == "True")
            {
                radioGroup_gioitinh_GV.SelectedIndex = 0;
            }
            else
            {
                radioGroup_gioitinh_GV.SelectedIndex = 1;
            }
        }

        private void Bindings()
        {
            txt_GVMaGV.DataBindings.Clear();
            txt_GVTenGV.DataBindings.Clear();
            txt_GVDienThoai.DataBindings.Clear();
            txt_GVEmail.DataBindings.Clear();
            txt_GVMaCN.DataBindings.Clear();
            txt_DSGVCNBM.DataBindings.Clear();
            txt_DSGVLCNC.DataBindings.Clear();
            txt_DSGVMaKhoa.DataBindings.Clear();

            txt_GVMaGV.DataBindings.Add("Text", grid_GiangVien.DataSource, "MaGV");
            txt_GVTenGV.DataBindings.Add("Text", grid_GiangVien.DataSource, "TenGV");

            txt_GVDienThoai.DataBindings.Add("Text", grid_GiangVien.DataSource, "DienThoai");
            txt_GVEmail.DataBindings.Add("Text", grid_GiangVien.DataSource, "Email");
            txt_GVMaCN.DataBindings.Add("Text", grid_GiangVien.DataSource, "MaCN");
            txt_DSGVCNBM.DataBindings.Add("Text", grid_GiangVien.DataSource, "CNBoMon");
            txt_DSGVLCNC.DataBindings.Add("Text", grid_GiangVien.DataSource, "LVCN");
            txt_DSGVMaKhoa.DataBindings.Add("Text", grid_GiangVien.DataSource, "MaKhoa");
 
        }

        private void simpleBt_Xoa_Click(object sender, EventArgs e)
        {
            bool kq;
            if (MessageBox.Show("Bạn có chắc sẽ xóa giảng viên " + txt_GVMaGV.Text + " hay không, nếu xóa thì toàn bộ thông thông tin liên quan sẽ mất.", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                kq = BUS_GiangVien.deleteGiangVien(txt_GVMaGV.Text);
                if (!kq)
                    XtraMessageBox.Show("Xóa thất bại", "Thông báo");
                else
                {
                    load();
                }
            }
            else
                load();


        }

        private void simpleBt_Them_Click(object sender, EventArgs e)
        {
            radioGroup_gioitinh_GV.Enabled = true;
            layoutMaCN_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaCN_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutMaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaKhoa_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_Capnhat.Enabled = true;
            simpleBt_Huy.Enabled = true;

            txt_GVMaGV.DataBindings.Clear();
            txt_GVTenGV.DataBindings.Clear();
            txt_GVDienThoai.DataBindings.Clear();
            //txt_GVGioiTinh.DataBindings.Clear();
            txt_GVEmail.DataBindings.Clear();
            txt_GVMaCN.DataBindings.Clear();
            txt_DSGVCNBM.DataBindings.Clear();
            txt_DSGVLCNC.DataBindings.Clear();
            txt_DSGVMaKhoa.DataBindings.Clear();

            txt_GVMaGV.Text = "";
            txt_GVTenGV.Text = "";
            txt_GVDienThoai.Text = "";
            //txt_GVGioiTinh.Text = "";
            txt_GVEmail.Text = "";
            txt_GVMaCN.Text = "";
            txt_DSGVCNBM.Text = "";
            txt_DSGVLCNC.Text = "";
            txt_DSGVMaKhoa.Text = "";


            txt_GVMaGV.Enabled = true;
            txt_GVTenGV.Enabled = true;
            txt_GVDienThoai.Enabled = true;
            //txt_GVGioiTinh.Enabled = true;
            txt_GVEmail.Enabled = true;
            txt_GVMaCN.Enabled = true;
            txt_DSGVCNBM.Enabled = true;
            txt_DSGVLCNC.Enabled = true;
            txt_DSGVMaKhoa.Enabled = true;


            TTThemSua = true;


        }

        private void simpleBt_Sua_Click(object sender, EventArgs e)
        {
            radioGroup_gioitinh_GV.Enabled = true;
            layoutMaCN_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaCN_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutMaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutMaKhoa_lookup.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            MaGV_cu = txt_GVMaGV.Text;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_Capnhat.Enabled = true;
            simpleBt_Huy.Enabled = true;



            txt_GVMaGV.Enabled = true;
            txt_GVTenGV.Enabled = true;
            txt_GVDienThoai.Enabled = true;
            //txt_GVGioiTinh.Enabled = true;
            txt_GVEmail.Enabled = true;
            txt_GVMaCN.Enabled = true;
            txt_DSGVCNBM.Enabled = true;
            txt_DSGVLCNC.Enabled = true;
            txt_DSGVMaKhoa.Enabled = true;


            txt_GVMaGV.Enabled = true;
            txt_GVTenGV.Enabled = true;
            txt_GVDienThoai.Enabled = true;
            //txt_GVGioiTinh.Enabled = true;
            txt_GVEmail.Enabled = true;
            txt_GVMaCN.Enabled = true;
            txt_DSGVCNBM.Enabled = true;
            txt_DSGVLCNC.Enabled = true;
            txt_DSGVMaKhoa.Enabled = true;

            TTThemSua = false;



        }

        private void simpleBt_Capnhat_Click(object sender, EventArgs e)
        {
            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_GiangVien GV = new DTO_GiangVien();
            GV.MaGV = txt_GVMaGV.Text;
            GV.TenGV = txt_GVTenGV.Text;
            GV.DienThoai = txt_GVDienThoai.Text;
            //GV.GioiTinh = bool.Parse(txt_GVGioiTinh.Text);
            GV.Email = txt_GVEmail.Text;
            GV.MaCN = gridLookMaCN.EditValue.ToString();
            GV.CNBoMon = txt_DSGVCNBM.Text;
            GV.LVCN = txt_DSGVLCNC.Text;
            GV.MaKhoa = gridLookMaKhoa.EditValue.ToString();
            if (radioGroup_gioitinh_GV.SelectedIndex == 0)
                GV.GioiTinh = true;
            else
                GV.GioiTinh = false;

            if (TTThemSua)
            {

                if (BUS_GiangVien.insertGiangVien(GV))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
            else
            {
                if (BUS_GiangVien.updateGiangVien(GV, MaGV_cu))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }

        }

        private bool KTDulieu()
        {

            if (txt_GVMaGV.Text == "")
                return false;
            if (txt_GVTenGV.Text == "")
                return false;
            if (txt_GVDienThoai.Text == "")
                return false;
            if (gridLookMaCN.EditValue == null)
                return false;
            if (txt_GVEmail.Text == "")
                return false;
            if (txt_DSGVCNBM.Text == "")
                return false;
            if (txt_DSGVLCNC.Text == "")
                return false;
            if (gridLookMaKhoa.EditValue == null)
                return false;
            return true;
            

        }

        private void txt_GVMaGV_EditValueChanged(object sender, EventArgs e)
        {
            if (txt_GVMaGV.Enabled == true)
                return;
            if (gv_GV.GetRowCellValue(gv_GV.FocusedRowHandle, "GioiTinh") == null)
                return;
            
            if (gv_GV.GetRowCellValue(gv_GV.FocusedRowHandle, "GioiTinh").ToString() == "True")
                radioGroup_gioitinh_GV.SelectedIndex = 0;
            else
                radioGroup_gioitinh_GV.SelectedIndex = 1;

        }

        private void simpleBt_Huy_Click(object sender, EventArgs e)
        {
            load();
        }

        private void txt_GVMaGV_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_GVTenGV.Focus();
        }

        private void txt_GVTenGV_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_GVDienThoai.Focus();
        }

        private void txt_GVDienThoai_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                radioGroup_gioitinh_GV.Focus();
        }

        private void radioGroup_gioitinh_GV_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_GVEmail.Focus();
        }

        private void txt_GVEmail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_GVMaCN.Focus();
        }

        private void txt_GVMaCN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSGVCNBM.Focus();
        }

        private void txt_DSGVCNBM_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSGVLCNC.Focus();
        }

        private void txt_DSGVLCNC_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_DSGVMaKhoa.Focus();
        }

        private void txt_DSGVMaKhoa_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                simpleBt_Capnhat.Focus();
        }

        private void txt_GVDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void gridLookMaCN_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}